package com.sf.fy.uav.applet.noice.controller;

import com.alibaba.fastjson.JSONObject;
import com.sf.fy.uav.applet.login.service.LoginService;
import com.sf.fy.uav.common.enums.ServicePrefixEnum;
import com.sf.fy.uav.common.utils.RedisPrefixKey;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * @author LTQ
 */
@RestController
@RequestMapping("/home/notice/")
public class NoticesController {

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginService loginService;

    @RequestMapping("send")
    public String send() {
/*
        JSONObject body = new JSONObject();
        body.put("touser", "用户的openId");
        body.put("template_id", "你的模板Id");
        JSONObject json = new JSONObject();
        json.put("thing6", new JSONObject().put("value", "20200820757539"));
        json.put("date4", new JSONObject().put("value", LocalDateTime.now()));
        json.put("amount3", new JSONObject().put("value", "多功能等一件商品"));
        json.put("character_string2", new JSONObject().put("value", new Date()));
        json.put("thing5", new JSONObject().put("value", "拼团"));
        //发送
        body.put("data", json);
//        String accessToken = getAccessToken();
        String accessToken = "";
        String post = restTemplate.postForEntity("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken, body.toString());
*/
        String openid = (String) redisTemplate.opsForValue().get(ServicePrefixEnum.RESCUE + RedisPrefixKey.APPLET_OPENID);
        String accessToken = loginService.getAccessToken();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser", openid);
        jsonObject.put("template_id", "qx_RfGPurM78ZlG4Qqa4RzIXZxlSAgo5mIIJsslmb6U");

        JSONObject json = new JSONObject();
        json.put("character_string1", new JSONObject().put("value", "121211asdfghjkloiuytrewq12345678"));
        json.put("date4", new JSONObject().put("value", DateUtils.formatDate(new Date())));
        json.put("phrase11", new JSONObject().put("value", "拼团"));
        json.put("thing5", new JSONObject().put("value", "拼团"));


        //发送
        jsonObject.put("data", json);
        HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
        ResponseEntity<JSONObject> response;
        response = restTemplate.postForEntity("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken, entity, JSONObject.class);
        JSONObject result = response.getBody();
//        JSONObject jsonObject = JSONObject.parseObject("{\n" +
//                "    \"requestId\": null,\n" +
//                "    \"success\": true,\n" +
//                "    \"errorCode\": null,\n" +
//                "    \"date\": \"2020-11-17 18:08:31\",\n" +
//                "    \"timestamp\": 1605607711793,\n" +
//                "    \"version\": 1.0,\n" +
//                "    \"errorMessage \": null,\n" +
//                "    \"obj \": null\n" +
//                "}");
        return "ok";
    }
}
